package com.jspgou.cms.dao.impl;

import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

import com.jspgou.common.hibernate3.Finder;
import com.jspgou.common.hibernate3.HibernateBaseDao;
import com.jspgou.common.page.Pagination;
import com.jspgou.cms.dao.CouponDao;
import com.jspgou.cms.entity.Coupon;

@Repository
public class CouponDaoImpl extends HibernateBaseDao<Coupon, Long> implements
		CouponDao {
	public Pagination getPage(Long storeId,int pageNo, int pageSize) {
		Finder f = Finder.create("from Coupon bean where 1=1");
		if (storeId != null) {
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		return find(f, pageNo, pageSize);
	}

		
	@SuppressWarnings("unchecked")
	public List<Coupon> getList(Long storeId){
		/*String hql="from Coupon bean where 1=1";
		return getSession().createQuery(hql).list();*/
		Finder f = Finder.create("from Coupon bean where 1=1");
		if (storeId != null) {
			f.append(" and bean.store.id=:storeId");
			f.setParam("storeId", storeId);
		}else{
			f.append(" and bean.store.id is null");
		}
		return find(f);
	}
		
	
	
	@SuppressWarnings("unchecked")
	public Coupon fetchCoupon(Double bd){//获得优惠劵(wang ze wu)
		Iterator<Coupon> it=this.getSession().createQuery("from Coupon bean where bean.leastPrice <=:orderPrice")
		.setDouble("orderPrice", bd).iterate();
		if(it.hasNext()){
			Coupon c=it.next();
			return c;
		}
		return null;
	}

	public Coupon findById(Long id) {
		Coupon entity = get(id);
		return entity;
	}

	public Coupon save(Coupon bean) {
		getSession().save(bean);
		return bean;
	}

	public Coupon deleteById(Long id) {
		Coupon entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	protected Class<Coupon> getEntityClass() {
		return Coupon.class;
	}
}